﻿using GrapeCity.Forguncy.CellTypes;
using GrapeCity.Forguncy.Commands;
using GrapeCity.Forguncy.Plugin;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using Vant.Properties;
using Vant.WpfControls.DrawingObject;

namespace Vant.CellTypes
{
    public class SliderDesigner : CellTypeDesigner<Slider>, ISupportPropertyInitialize
    {
        public override FrameworkElement GetDrawingControl(ICellInfo cellInfo, IDrawingHelper drawingHelper)
        {
            return new SliderDrawingObject(this.CellType, drawingHelper);
        }

        public void InitDefaultPropertyValues(IBuilderContext context)
        {
            this.CellType.min = 0;
            this.CellType.max = 100;
            this.CellType.step = 1;
            this.CellType.barHeight = 2;
            this.CellType.buttonSize = 24;
            this.CellType.activeColor = "Accent 1";
            this.CellType.inactiveColor = "#e5e5e5";
        }
    }

    [Designer("Vant.CellTypes.SliderDesigner, Vant")]
    [Icon("pack://application:,,,/Vant;component/Resources/Images/Slider.png")]
    [OrderWeight((int)Utils.OrderWeight.Slider)]
    public class Slider : VantCellTypeBase, ICommandCellType, ISupportUIPermission, ISupportDefaultValue, ISupportDisable, ISupportReadOnly
    {
        [SRDisplayName(nameof(Resources.Vant_EditCommands))]
        public List<Command> CommandList { get; set; } = new List<Command>();
        public CommandExcuteKind CommandExcuteKind => CommandExcuteKind.OnValueChanged;

        List<UIPermission> _uIPermissions;
        [SRDisplayName(nameof(Resources.Vant_UIPermissions))]
        [JsonProperty(ObjectCreationHandling = ObjectCreationHandling.Replace)]
        public List<UIPermission> UIPermissions
        {
            get
            {
                if (_uIPermissions == null)
                {
                    _uIPermissions = Utils.GetDefaultPermission(UIPermissionScope.Enable, UIPermissionScope.Visible, UIPermissionScope.Editable);
                }
                return _uIPermissions;
            }
            set
            {
                _uIPermissions = value;
            }
        }

        [FormulaProperty]
        [SRDisplayName(nameof(Resources.Vant_DefaultValue))]
        public object DefaultValue { get; set; }

        [Browsable(false)]
        [JsonIgnore]
        public bool NeedFormatDefaultValue => false;

        [FormulaProperty]
        [SRDisplayName(nameof(Resources.Slider_Min))]
        public object min { get; set; }

        [FormulaProperty]
        [SRDisplayName(nameof(Resources.Slider_Max))]
        public object max { get; set; }

        [FormulaProperty]
        [SRDisplayName(nameof(Resources.Slider_Step))]
        public object step { get; set; }

        [DefaultValue(false)]
        [SRDisplayName(nameof(Resources.Slider_Layout))]
        [SRRadioGroupProperty(ValueList = "false|true", DisplayList = nameof(Resources.Slider_Layout_DisplayList))]
        public bool vertical { get; set; } = false;

        [IntProperty(Min = 0)]
        [SRDisplayName(nameof(Resources.Slider_BarHeight))]
        public int barHeight { get; set; }

        [IntProperty(Min = 0)]
        [SRDisplayName(nameof(Resources.Slider_ButtonSize))]
        public int buttonSize { get; set; }

        [ColorProperty]
        [SRDisplayName(nameof(Resources.Slider_ActiveColor))]
        public string activeColor { get; set; }

        [ColorProperty]
        [SRDisplayName(nameof(Resources.Slider_InactiveColor))]
        public string inactiveColor { get; set; }

        [SRCategoryHeader(nameof(Resources.Other))]
        [SRDisplayName(nameof(Resources.Slider_EnableRange))]
        [DefaultValue(false)]
        public bool range { get; set; } = false;

        [SRDisplayName(nameof(Resources.Slider_Reverse))]
        [DefaultValue(false)]
        public bool reverse { get; set; } = false;

        [SRDisplayName(nameof(Resources.Vant_Disable))]
        [DefaultValue(false)]
        public bool IsDisabled { get; set; } = false;

        [SRDisplayName(nameof(Resources.Vant_Readonly))]
        [DefaultValue(false)]
        public bool ReadOnly { get; set; } = false;

        public override SupportFeatures SupportFeatures
        {
            get
            {
                return SupportFeatures.ShouldCheckDirtyWhenLeavePage;
            }
        }

        public override string ToString()
        {
            return Resources.Slider_DisplayName;
        }
    }
}
